﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BalanceCenter_ActivationUserList : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //if (!_User.isSurrogate)
            //{
            //    PublicFunction.GoError(ErrorNumber.Unknow, "对不起，您不是代理商用户，没有此项操作功能", this.GetType().BaseType.FullName);

            //    return;
            //}

            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        //SecondLevelPassword = true;
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        RequestCompetences = Competences.BuildCompetencesList(Competences.SurrogateA);//页面权限

        base.OnInit(e);
    }

    #endregion

    private void BindData()
    {
        DataTable dt = null;

        if (_Customer.Competences.IsOwnedCompetences(Competences.Administrator))
        {
            dt = new DAL.Views.V_Customer().Open("", "isActivation = 0 ", "");
        }
        else
        {
            dt = new DAL.Views.V_Customer().Open("", "isActivation = 0 and SurrogateID = " + _Customer.ID.ToString(), "");
        }

        if (dt == null)
        {  
            return;
        }

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
    }

    protected void g_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            DataRow dr = drv.Row;

            int CustomerTypeID = Shove._Convert.StrToInt(dr["CustomerTypeID"].ToString(), 0);

            if (CustomerTypeID == 1)
            {
                e.Item.Cells[13].Text = PublicFunction.GetOptionsAsString("每单价格");
            }

            e.Item.Cells[12].Text = Shove._Convert.StrToDateTime(e.Item.Cells[12].Text, System.DateTime.Now.ToString()).ToShortDateString();            
        }
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void btnSerch_Click(object sender, EventArgs e)
    {
        if (this.tbSerch.Text.Trim().Equals(""))
        {
            Shove._Web.JavaScript.Alert(this, "请输入要搜索的会员ID");

            return;
        }

        Customer Customer = new Customer(_Site.ID);
        Customer.CustomerNO = this.tbSerch.Text.Trim();
        string ret = "";
        int result = Customer.GetCustomerInformationByNO(ref ret);

        if (result == -1)
        {
            Shove._Web.JavaScript.Alert(this, "此会员不存在");
            return;
        }

        DataTable dt = new DAL.Views.V_Customer().Open("", "isActivation = 0 and SurrogateID = " + _Customer.ID.ToString() + " and [ID] = " + Customer.ID.ToString(), "");

        if (dt == null)
        {
            PublicFunction.GoError(ErrorNumber.DataReadWrite, "数据读写错误", this.GetType().BaseType.FullName);

            return;
        }

        gPager.PageIndex = 0;

        g.DataSource = dt;
        g.DataBind();
        gPager.Visible = (dt.Rows.Count > 0);
    }

    protected void btnSerchAll_Click(object sender, EventArgs e)
    {
        gPager.PageIndex = 0;

        BindData();
    }

    protected void g_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        long CustomerID = Shove._Convert.StrToLong(e.Item.Cells[15].Text, -1);

        string ReturnDescription = "";

        if (e.CommandName == "Activation")
        {
            if (DAL.Procedures.P_ActivationCustomer(CustomerID, _Customer.ID, ref ReturnDescription) < 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, ReturnDescription);

                return;
            }

            //BindData();
            Response.Redirect("Net.aspx");

            return;
        }

        if (e.CommandName == "EditUserNet")
        {
            Response.Redirect("EditUserNet.aspx?id=" + CustomerID.ToString(), true);

            return;
        }

        if (e.CommandName == "Del")
        {
            if (DAL.Procedures.P_DelCustomer(CustomerID, ref ReturnDescription) < 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, ReturnDescription);

                return;
            }

            BindData();

            return;
        }
    }
}